using System;
using System.Data;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using CloudApiServer.Model;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;

namespace CloudApiServer.Controllers;

/// <summary>
/// 医保一码付接口控制器
/// </summary>
[RoutePrefix("cfc/api")]
public class FsiCfcController : ApiController
{
    /// <summary>
    /// 医保接口没找到的统一接口返回
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpGet]
    [HttpPost]
    public IHttpActionResult FsiNotFound(HttpRequestMessage request)
    {
        string url = request.RequestUri.ToString();
        string Method = request.Method.ToString();
        string Errnum = LogHelper.GetErrornumber();
        try
        {
            FsiCfcHeader FsiHeader = new FsiCfcHeader
            {
                code = 50000,
                message =  $"[{Errnum}]调用电子凭证一码付自定义服务失败！请求的接口【{url}】方法【{Method}】暂未添加,当前请求入参：" + request.Content.ReadAsStringAsync().Result,
                success = false
            };
            return Json(FsiHeader);
        }
        catch (Exception ex)
        {
            FsiCfcHeader FsiHeader = new FsiCfcHeader
            {
                code = 50000,
                message = $"[{Errnum}]调用电子凭证一码付自定义服务失败！请检查！错误内容：处方中心接口异常！错误内容：{ex.Message} 异常来源：{ex.StackTrace} " ,
                success = false
            };
            return Json(FsiHeader);
        }
    }
    /// <summary>
    /// 查询用户一码付状态
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [Route("signedQuery")]
    [HttpPost]
    public async Task<IHttpActionResult> signedQuery(HttpRequestMessage request)
    {
        try
        {
            // 判断接口是否被控制台停用
            if (FsiDatainfo.stateFsiCfcApi == false)
            {
                throw new Exception("该接口已被控制台停用！请联系管理员！");
            }

            var fsiRequest = JsonConvert.DeserializeObject<FsiRequestCfcHeader>(request.Content.ReadAsStringAsync().Result);
            var encData = fsiRequest.encData;
            /*
            // 解密encData
            var sm4Decrypt = new SM4Util(FsiCfcTokenInfo.key, false);
            var decrypted = sm4Decrypt.Decrypt(encData);
            //解析入参 处理业务逻辑 暂时没有
            var decryptedData = JsonConvert.DeserializeObject<FsiRequestCfcSignedQueryInput>(decrypted);
            */
            var FsiCfcSignedQueryOutput = new FsiCfcSignedQueryOutput()
            {
                stasFlag = "0",
                abnRea = "无异常",
                payChnlUserName = "123",
                payChnlName = "支付宝"
            };
            var sm4Encrypt = new SM4Util(FsiCfcTokenInfo.key);
            var returnEncData = sm4Encrypt.Encrypt(JsonConvert.SerializeObject(FsiCfcSignedQueryOutput, Formatting.None));
            FsiCfcHeader FsiHeader = new FsiCfcHeader
            {
                code = 0,
                message = "success",
                data = new FsiCfcResponseData()
                {
                    encData = returnEncData
                },
                type = "0",
                success = true
            };
            return Json(FsiHeader);


        }
        catch (Exception ex)
        {
            string Errnum = LogHelper.GetErrornumber();
            var FsiHeader = new FsiCfcHeader
            {
                code = -1,
                message = $"[{Errnum}]调用电子凭证一码付自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}",
                success = false
            };
            LogHelper.Error($"[{Errnum}]调用电子凭证一码付自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}");
            return Json(FsiHeader);
        }
    }
    
    /// <summary>
    /// 获取一码付付款凭据
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [Route("genPayCert")]
    [HttpPost]
    public async Task<IHttpActionResult> genPayCert(HttpRequestMessage request)
    {
        try
        {
            // 判断接口是否被控制台停用
            if (FsiDatainfo.stateFsiCfcApi == false)
            {
                throw new Exception("该接口已被控制台停用！请联系管理员！");
            }
            
            var fsiRequest = JsonConvert.DeserializeObject<FsiRequestCfcHeader>(request.Content.ReadAsStringAsync().Result);
            var encData = fsiRequest.encData;
            /*
            // 解密encData
            var sm4Decrypt = new SM4Util(FsiCfcTokenInfo.key, false);
            var decrypted = sm4Decrypt.Decrypt(encData);
            //解析入参 处理业务逻辑 暂时没有
            var decryptedData = JsonConvert.DeserializeObject<FsiRequestCfcGenPayCertInput>(decrypted);
            */
            var FsiCfcGenPayCertOutput = new FsiCfcGenPayCertOutput()
            {
                payCert = "123123123",
                payChnlUserName = "123",
                payChnlName = "支付宝"
            };
            var sm4Encrypt = new SM4Util(FsiCfcTokenInfo.key);
            var returnEncData = sm4Encrypt.Encrypt(JsonConvert.SerializeObject(FsiCfcGenPayCertOutput, Formatting.None));
            FsiCfcHeader FsiHeader = new FsiCfcHeader
            {
                code = 0,
                message = "success",
                data = new FsiCfcResponseData()
                {
                    encData = returnEncData
                },
                type = "0",
                success = true
            };
            return Json(FsiHeader);
        }
        catch (Exception ex)
        {
            string Errnum = LogHelper.GetErrornumber();
            var FsiHeader = new FsiCfcHeader
            {
                code = -1,
                message = $"[{Errnum}]调用电子凭证一码付自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}",
                success = false
            };
            LogHelper.Error($"[{Errnum}]调用电子凭证一码付自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}");
            return Json(FsiHeader);
        }
    }
    /// <summary>
    /// 测试解密
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [Route("TestDecrypt")]
    [HttpPost]
    public async Task<IHttpActionResult> TestDecrypt(HttpRequestMessage request)
    {
        try
        {
            var fsiRequest = JsonConvert.DeserializeObject<FsiCfcHeader>(request.Content.ReadAsStringAsync().Result);
            var encData = fsiRequest.data.encData;
            // 解密encData
            var sm4Decrypt = new SM4Util(FsiCfcTokenInfo.key, false);
            var decrypted = sm4Decrypt.Decrypt(encData);
            return Ok(decrypted);
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }
}